package com.css.fxfzysx.modules.lifeline.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.lifeline.param.LifelineFoundationLibraryParam;
import com.css.fxfzysx.modules.lifeline.param.updateBasicLibraryParam;
import com.css.fxfzysx.modules.lifeline.service.LifelineFoundationLibraryService;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("lifelineFoundationLibrary")
@Slf4j
public class LifelineFoundationLibraryController {

    @Autowired
    private LifelineFoundationLibraryService lifelineFoundationLibraryService;

    /**
     * 导入数据
     */
    @PostMapping("upload")
    public RestResponse upload(@RequestParam("type") Integer type,
                               @RequestParam("province") String province,
                               @RequestParam("city") String city,
                               @RequestParam("county") String county,
                               @RequestParam MultipartFile file) {
        try {
            if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
                return RestResponse.fail("文件格式不支持!");
            }
            return lifelineFoundationLibraryService.upload(type, province, city, county, file);
        } catch (Exception e) {
            log.error("post uploadUpNetwork error:{}", e.getMessage(), e);
            return RestResponse.fail("导入失败!");
        }
    }

    /**
     * 分页
     *
     * @param param
     * @param request
     * @return
     */
    @GetMapping("page")
    public RestResponse page(LifelineFoundationLibraryParam param, HttpServletRequest request) {
        try {
            int page = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            HashMap<String, Object> map = lifelineFoundationLibraryService.page(param, page, size);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get page error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 风险区划  根据生命线类型和id查询
     *
     * @param type
     * @param id
     * @return
     */
    @GetMapping("getByTypeAndLifelineIdAndIntensity")
    public DestructiveStateVo getByTypeAndLifelineIdAndIntensity(String id,Integer type,Integer range) {
        try {
            return lifelineFoundationLibraryService.getByTypeAndLifelineIdAndIntensity(id, type,range);
        } catch (Exception e) {
            log.error("get getByTypeAndLifelineIdAndIntensity error:{}", e.getMessage(), e);
            return new DestructiveStateVo();
        }
    }

    /**
     * 修改
     */
    @PostMapping("updateById")
    public RestResponse updateById(@RequestBody List<updateBasicLibraryParam> list){
        try {
            lifelineFoundationLibraryService.updateById(list);
            return RestResponse.succeed("修改成功!");
        } catch (Exception e) {
            log.error("post updateById error:{}",e.getMessage(),e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @GetMapping("deleteByTypeAndIds")
    public RestResponse deleteByIds(Integer type,String ids){
        try {
            lifelineFoundationLibraryService.deleteByTypeAndIds(type,ids);
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            log.error("get deleteByTypeAndIds error:{}",e.getMessage(),e);
            return RestResponse.fail("删除失败!");
        }
    }

    /**
     * 查询是省级管理员还是国家级管理员
     */
    @GetMapping("getRole")
    public RestResponse getRole(){
        try {
            Integer role = lifelineFoundationLibraryService.getRole();
            return RestResponse.succeed(role);
        } catch (Exception e) {
            log.error("get getRole error:{}",e.getMessage(),e);
            return RestResponse.fail("查询失败!");
        }
    }

}
